package com.itheima.app.controller;

import com.itheima.app.config.R;
import com.itheima.health.controller.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validateCode")
public class validateCodeController {
    @Autowired
    private StringRedisTemplate redisTemplate;


  @PostMapping("/send4Order")
    public R send4Order(@RequestParam("telephone") String telephone, HttpSession session)throws Exception{
      Random random = new Random();
      //生成随机数
      int rannum = random.nextInt(9999)+1000;
      System.out.println(rannum);
      Sample.duanxin(telephone, String.valueOf(rannum));
      redisTemplate.opsForValue().set(telephone,String.valueOf(rannum),5, TimeUnit.MINUTES);
      session.setAttribute("validateCode",rannum);
      return R.success("发送成功");
  }

}
